//
//  untitled.m
//  iTunesToTwitter
//
//  Created by Benjamin Ruoff on 11.05.09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//updateIt:	(Modeled Method)	MainMenu.xib

#import "MyController.h"

@implementation MyController
- (id)init
{
	self = [super init];
	if(self) 
	{
		[[NSDistributedNotificationCenter defaultCenter] addObserver:self 
															selector:@selector(iTunesChanged:) 
																name:@"com.apple.iTunes.playerInfo" 
															  object:nil];
	}
	return self;
}

- (void) iTunesChanged:(NSNotification *)aNotification 
{
	NSDictionary *userInfo    = [aNotification userInfo];
	DataStore* dataStore = [DataStore instance];
	
	[dataStore setTrack:[userInfo valueForKey:@"Name"]];
	[dataStore setArtist:[userInfo valueForKey:@"Artist"]];
	
	if ([dataStore getUpdateStatus] != 0)
	{
		//Update Twitter Status with actual iTunes Track
		if([@"" caseInsensitiveCompare:[dataStore getLogin]] == NSOrderedSame || [@"" caseInsensitiveCompare:[dataStore getPassword]] == NSOrderedSame )
		{
			NSAlert* alert = [[NSAlert alloc] init] ;
			[alert setMessageText: @"Wichtige Mitteilung"];
			[alert setInformativeText: @"Bitte vervollständigen Sie ihre Zugangsdaten"];
			[alert setAlertStyle:NSInformationalAlertStyle];
			[alert runModal];
			[alert release]; 
		}
		else
		{
			TwitterWrapper* twitterWrapper = [TwitterWrapper instance];
			[twitterWrapper sendUpdate:[userInfo valueForKey:@"Name"]:[userInfo valueForKey:@"Artist"]];
		}
	}
		
	NSLog(@"%@",[userInfo valueForKey:@"Name"]);
	[currentTrack setStringValue:[userInfo valueForKey:@"Name"]];
    [currentArtist setStringValue:[userInfo valueForKey:@"Artist"]];
	
}
@end
